<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { logImageCoordinates, logImageSize } from '@/util/imageCoordinateUtil';

// 定义单词数据，包括名称和在图片上的坐标
const words = ref([
  { id: 1, name: 'one', coords: '165,692,370,865' },   // 示例坐标：左上角区域
  { id: 2, name: 'two', coords: '949,533,1178,692' },  // 示例坐标：上方中间区域
  { id: 3, name: 'three', coords: '1357,417,1537,535' },  // 示例坐标：右上角区域
  { id: 4, name: 'four', coords: '1641, 402,1878, 533' },  // 示例坐标：左中侧区域
  { id: 5, name: 'five', coords: '2220, 359,2483, 495' }, // 示例坐标：中间区域
  { id: 6, name: 'six', coords: '967,1051,1213, 1227' }, // 示例坐标：右中侧区域
  { id: 7, name: 'seven', coords: '1340, 1120,1586, 1245' },   // 示例坐标：左上角区域
  { id: 8, name: 'eight', coords: '1968, 770,2171, 981' },  // 示例坐标：上方中间区域
  { id: 9, name: 'nine', coords: '2237, 935,2515, 1056' },  // 示例坐标：右上角区域
  { id: 10, name: 'ten', coords: '2654, 726,2938, 889' },  // 示例坐标：左中侧区域
  { id: 11, name: 'eleven', coords: '243, 1601,559, 1800' }, // 示例坐标：中间区域
  { id: 12, name: 'twelve', coords: '1042, 1598,1383, 1795' }, // 示例坐标：右中侧区域
  { id: 13, name: 'thirteen', coords: '1574, 1566,1838, 1737' },   // 示例坐标：左上角区域
  { id: 14, name: 'fourteen', coords: '2214, 1572,2489, 1731' },  // 示例坐标：左中侧区域
  { id: 15, name: 'fifteen', coords: '2778, 1609,3039, 1777' }, // 示例坐标：中间区域
  { id: 16, name: 'sixteen', coords: '203, 1991,408, 2148' }, // 示例坐标：右中侧区域
  { id: 17, name: 'seventeen', coords: '585, 2032,854, 2174' },   // 示例坐标：左上角区域
  { id: 18, name: 'eighteen', coords: '1140, 2038,1424, 2185' },  // 示例坐标：上方中间区域
  { id: 19, name: 'nineteen', coords: '1786, 2029,2032, 2165' },  // 示例坐标：右上角区域
  { id: 20, name: 'twenty', coords: '2483, 1965,2833, 2148' },  // 示例坐标：上方中间区域
]);
const  isDebug = false;
// 图片引用
const imageRef = ref(null);
// 控制对话框显示
const showDialog = ref(false);
// 翻译链接
const translateUrl = ref('');

// 处理单词点击事件
const handleWordClick = (word) => {
  console.log('点击了单词:', word.name);
  alert(`你点击了单词: ${word.name}------`);
  // 这里可以添加更多处理逻辑，比如发音、翻译等
};

// 检查点击位置是否在某个单词区域内
const isPointInWordArea = (x, y, coords) => {
  const [x1, y1, x2, y2] = coords.split(',').map(Number);
  return x >= x1 && x <= x2 && y >= y1 && y <= y2;
};

// 获取点击位置的单词信息或坐标
const getClickInfo = (event) => {
  if (imageRef.value) {
    // 获取点击坐标
    const coords = logImageCoordinates(event, imageRef.value);
    const { originalX, originalY } = coords;
    
    // 检查点击位置是否在某个单词区域内
    const clickedWord = words.value.find(word => 
      isPointInWordArea(originalX, originalY, word.coords)
    );
    
    if (clickedWord) {
      // 如果点击位置在某个单词区域内，显示该单词信息和坐标
      if(isDebug){
        alert(`你点击了单词: ${clickedWord.name} (ID: ${clickedWord.id})\n坐标: ${clickedWord.coords}`);
        console.log('点击了单词:', clickedWord);
      }else{
        // 构造百度翻译的URL路径
        translateUrl.value = `https://fanyi.baidu.com/mtpe-individual/transText?query=${clickedWord.name}&lang=en2zh&ext_channel=DuSearch#/`;
        // 显示对话框
        showDialog.value = true;
      }
    } else {
      // 如果点击位置不在任何单词区域内，显示点击坐标
      alert(`点击坐标: (${originalX}, ${originalY})`);
    }
  }
};

// 关闭对话框
const closeDialog = () => {
  showDialog.value = false;
};

// 获取图片原始尺寸
onMounted(() => {
  if (imageRef.value) {
    // 使用工具类方法打印图片尺寸信息
    logImageSize(imageRef.value);
  }
});
</script>

<template>
  <div class="image-container">
    <img 
      ref="imageRef"
      src="@/assets/danci/_1numbers_6_7.jpg" 
      alt="Numbers 6-7" 
      class="full-screen-image" 
      usemap="#wordMap"
      @click="getClickInfo"
    />
    
    <!-- 图像映射区域 -->
    <map name="wordMap">
      <area
        v-for="word in words"
        :key="word.id"
        :alt="word.name"
        :title="word.name"
        :coords="word.coords"
        shape="rect"
        @click="handleWordClick(word)"
        class="clickable-area"
      />
    </map>
    
    <!-- 弹出对话框 -->
    <div v-if="showDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog-content" @click.stop>
        <div class="dialog-header">
          <span>翻译结果</span>
          <button class="close-btn" @click="closeDialog">✕</button>
        </div>
        <div class="dialog-body">
          <iframe 
            :src="translateUrl" 
            class="translate-frame"
            frameborder="0"
          ></iframe>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.image-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
}

.full-screen-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transform: rotate(180deg);
}

/* 可选：为调试目的添加可视化区域 */
.clickable-area {
  cursor: pointer;
  /* 如果需要可视化点击区域，可以取消下面的注释 */
  /* background: rgba(255, 0, 0, 0.1); */
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background-color: white;
  border-radius: 8px;
  width: 80%; /* 宽度占屏幕的70% */
  height: 80%; /* 高度占屏幕的80% */
  display: flex;
  flex-direction: column;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #eee;
}

.dialog-header span {
  font-size: 18px;
  font-weight: bold;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog-body {
  padding: 16px;
  flex: 1;
  overflow: hidden;
}

.translate-frame {
  width: 100%;
  height: 100%; /* 高度设为100%以填充对话框内容区域 */
}
</style>